package exercise.chapter9.ex9_1;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.util.StopWatch;

public class Counter implements Runnable
{
	@Override
	public void run()
	{
		StopWatch watch = new StopWatch();
		SimpleDateFormat sdf = new SimpleDateFormat("ss:SS");
		
		for (int i = 0; i < 100; i++)
		{
			watch.start();
			try
			{
				Thread.sleep(50);
			} 
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			watch.stop();
			if (i % 10 == 0)
			{
				System.out.println("------------------------");
				System.out.println(i);
				String time = sdf.format(new Date(watch.getLastTaskTimeMillis()));
				System.out.println(String.format("[%s]:Time taken by [%s]:[%s]", Thread.currentThread().getName(),i, time));
			}
		}
	}

}
